import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread(r"C:\Users\Public\opencv\Figure\lena.jpg")  #读入带有椒盐噪声的图像
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  # 转化为灰度图像

# 进行拉普拉斯算子运算
lap = cv2.Laplacian(img,cv2.CV_16S,ksize=3)
# 求绝对值并转为8比特图像
laplacian = cv2.convertScaleAbs(lap)


# 使用自定义的卷积函数
kernel3 = np.array([[-1,-1,0], [-1,0,1],[0,1,1]])
img2 = cv2.filter2D(img_gray,-1,kernel3)



plt.subplot(131),plt.imshow(img)
plt.subplot(132),plt.imshow(laplacian)
plt.subplot(133),plt.imshow(img2)


cv2.waitKey()
cv2.destroyAllWindows()